package net.ck.hzq;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import activity.glide.GlideApp;
import util.LogUtils;
import util.StringUtils;

public class WebviewActivity extends Activity {

    private WebView webview;
    private TextView title;
    private ImageView imageview;
    String type;

    private Handler handler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            switch (msg.what) {
                case 0x123:

                    break;
            }
        }
    };

    public static void start(Context context, String url) {
        Intent intent = new Intent(context, WebviewActivity.class);
        intent.putExtra("url", url);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        TobuyApplication.getmInstance().onActivityCreate(this);
        type = StringUtils.isEmpty(getIntent().getStringExtra("type")) ? "" : getIntent().getStringExtra("type");
        webview = (WebView) findViewById(R.id.webview);
        imageview = (ImageView)findViewById(R.id.imageview);
        title = findViewById(R.id.title);
        if (type.equals("1")) {
            title.setText("好钻钱合作协议及营销规范");
        } else if (type.equals("2")) {
            title.setText("办卡详情");
        } else {
            if(!StringUtils.isEmpty(type)){
                title.setText(type);
            }
        }
        WebSettings webSettings = webview.getSettings();

        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小

        webSettings.setSupportZoom(false); //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(true);
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setDomStorageEnabled(true);
        webview.getSettings().setBlockNetworkImage(false);
        String url = getIntent().getStringExtra("url");
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        LogUtils.d(url);

        webview.loadUrl(url);
//        webview.loadUrl("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
//        if(url.contains("png")||url.contains("jpg")||url.contains("jpeg")){
//            webview.setVisibility(View.GONE);
//            imageview.setVisibility(View.VISIBLE);
//            GlideApp.with(this)
//                    .load(url)
//                    .into(imageview);
//        }
//        webview.setWebViewClient(new WebViewClient() {
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                if (url.startsWith("http:")||url.startsWith("https:")) {
//                    view.loadUrl(url);
//                    return true;
//                }
//                return false;
//            }
//
//            @Override
//            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
//                handler.proceed();
//            }
//        });
        findViewById(R.id.iv_back).setOnClickListener(v -> finish());
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }
}
